// CryptoJS 加密
const CryptoJS = require('crypto-js');
// 加载配置文件
import config  from '../user-defined.config.js'
const { encrypts:{AES:{
	key:aesKey,
	iv:aesIv
}} } = config


export function test (){
	
}

// AES 加密
function encrypt(data='',key=aesKey,iv=aesIv){
	const transUtf8Key = CryptoJS.enc.Utf8.parse(key);
	const transUtf8Iv = CryptoJS.enc.Utf8.parse(iv);
	const encrypetPwd = CryptoJS.AES.encrypt(data, transUtf8Key, {
		iv: transUtf8Iv,
		mode: CryptoJS.mode.CFB,
		padding: CryptoJS.pad.NoPadding
	}).toString();
	
	
	
	return encrypetPwd  || ''
	
}
// AES 解密
function decrypt(data='',key=aesKey,iv=aesIv){
	const transUtf8Key = CryptoJS.enc.Utf8.parse(key);
	const transUtf8Iv = CryptoJS.enc.Utf8.parse(iv);
	const decrypetPwd = CryptoJS.AES.decrypt(data, transUtf8Key, {
		iv: transUtf8Iv,
		mode: CryptoJS.mode.CFB,
		padding: CryptoJS.pad.NoPadding
	})
	return decrypetPwd.toString(CryptoJS.enc.Utf8)
}


//休眠
function sleep (n=1000){
	let startTime = new Date().getTime()
	while(true){
		let curTime = new Date().getTime()
		if((curTime - startTime) > n ){
			break
		}
	}
}

function clone(obj){
	
	let buff ; 
	if(obj instanceof Array){
		buff = []
		let len = obj.length
		while(len --){
			buff[len] = clone(obj[len])
		}
		return buff
	}else if(obj instanceof Object){
		 buff = {}
		 for (let i in obj) {
		 	buff[i]  = clone(obj[i])
		 }
		 return buff
	}else{
		return obj
	}
	
	
}

export default {
	encrypt,
	decrypt,
	sleep,
	clone
}